package com.zzz.springsecuritystudy.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzz.springsecuritystudy.entity.ResponseResult;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * response 工具类
 * @date 2021/9/15
 */
public class ResponseUtils {

    public static void print(HttpServletResponse response, ResponseResult message) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            response.setStatus(HttpStatus.OK.value());
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            response.setHeader("Cache-Control", "no-cache, must-revalidate");
            PrintWriter writer = response.getWriter();
            writer.write(objectMapper.writeValueAsString(message));
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
